package com.ssh.dates.dao;

import java.sql.SQLException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.ssh.dates.model.DateTableNew;
import com.ssh.utils.DBConnection;

public class DateTimeNewOperation {

	@Test
	public void insert() {
		String sql = "insert into dateTable values (dateTable_id.nextval, " + 
						"to_date(?, 'yyyy-mm-dd'), " + 
						"to_date(?, 'yyyy-mm-dd hh24:mi:ss'), " + 
						"to_timestamp(?, 'yyyy-mm-dd hh24:mi:ss.ff'))";
		LocalDate ld = LocalDate.now();
		DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		LocalDateTime lt = LocalDateTime.now();
		DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		LocalDateTime ldt = LocalDateTime.now();
		DateTimeFormatter dtf3 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
		
		int result = DBConnection.update(sql, new Object[] {dtf1.format(ld),dtf2.format(lt),dtf3.format(ldt)});
		System.out.println(result);
	}
	@Test
	public void select() {
		String sql = "select * from dateTable";
		List<DateTableNew> list = DBConnection.query(sql, (x)->{
			List<DateTableNew> list0 = new ArrayList<>();
			try {
				while(x.next()) {
					DateTableNew dtn = new DateTableNew(x.getInt(1),x.getDate(2).toLocalDate(),x.getTime(3).toLocalTime(),x.getTimestamp(4).toLocalDateTime());
					list0.add(dtn);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return list0;
		}, new Object[] {});
		
		
		list.forEach((item->System.out.println(item.getDATECOLUMN()+"====="+item.getTIMECOLUMN()+"===="+item.getTIMESTAMPCOLUMN())));
	}
}
